package com.zhku.onlineshop.Servlet;

import com.zhku.onlineshop.Pojo.Address;
import com.zhku.onlineshop.Pojo.Result;
import com.zhku.onlineshop.Service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/Address")
public class AddressServlet {
    @Autowired
    private AddressService addressService;

    @PostMapping("/Add")
    public Result AddAddress(@RequestBody Address address){
        log.info(String.valueOf(address));
        log.info("添加地址");
        if (addressService.AddAddress(address))
            return Result.success();
        else
            return Result.error("添加地址失败");
    }

    @PostMapping("/Del")
    public Result DelAddressById(Integer id){
        log.info("删除地址id："+id);
        if(addressService.DelAddressById(id))
            return Result.success();
        else
            return Result.error("删除失败");
    }

    @PostMapping("/Upd")
    public Result UpdAddressById(@RequestBody Address address){
        log.info("修改地址id："+address);
        if(addressService.UpdAddressById(address))
            return Result.success();
        else
            return Result.error("修改失败");
    }

    @GetMapping("/Sel")
    public Result SelAddressByUserId(Integer user_id){
        log.info("查询地址：用户id："+user_id);
        List<Address> addressList = addressService.SelAddressById(user_id);
        if(addressList!=null)
            return Result.success(addressList);
        else
            return Result.error("修改失败");
    }

    @GetMapping("/GetByid")
    public Result SelAddressByAddressId(Integer id){
        log.info("查询地址：id："+id);
        List<Address> addressList = addressService.SelAddressByAddressId(id);
        if(addressList!=null)
            return Result.success(addressList);
        else
            return Result.error("修改失败");
    }

}
